
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Managers &#8212; Django 2.2.12.dev20200304094918 documentation</title>
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
    <link rel="next" title="Performing raw SQL queries" href="sql.html" />
    <link rel="prev" title="Search" href="search.html" />



 
<script type="text/javascript" src="../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../index.html">Django 2.2.12.dev20200304094918 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../index.html">Home</a>  |
        <a title="Table of contents" href="../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../genindex.html">Index</a>  |
        <a title="Module index" href="../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="search.html" title="Search">previous</a>
     |
    <a href="../index.html" title="Using Django" accesskey="U">up</a>
   |
    <a href="sql.html" title="Performing raw SQL queries">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="topics-db-managers">
            
  <div class="section" id="s-managers">
<span id="managers"></span><h1>Managers<a class="headerlink" href="#managers" title="Permalink to this headline">¶</a></h1>
<dl class="class">
<dt id="django.db.models.Manager">
<em class="property">class </em><code class="descname">Manager</code><a class="reference internal" href="../../_modules/django/db/models/manager.html#Manager"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.Manager" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>A <code class="docutils literal notranslate"><span class="pre">Manager</span></code> is the interface through which database query operations are
provided to Django models. At least one <code class="docutils literal notranslate"><span class="pre">Manager</span></code> exists for every model in
a Django application.</p>
<p>The way <code class="docutils literal notranslate"><span class="pre">Manager</span></code> classes work is documented in <a class="reference internal" href="queries.html"><span class="doc">Making queries</span></a>;
this document specifically touches on model options that customize <code class="docutils literal notranslate"><span class="pre">Manager</span></code>
behavior.</p>
<div class="section" id="s-manager-names">
<span id="s-id1"></span><span id="manager-names"></span><span id="id1"></span><h2>Manager names<a class="headerlink" href="#manager-names" title="Permalink to this headline">¶</a></h2>
<p>By default, Django adds a <code class="docutils literal notranslate"><span class="pre">Manager</span></code> with the name <code class="docutils literal notranslate"><span class="pre">objects</span></code> to every Django
model class. However, if you want to use <code class="docutils literal notranslate"><span class="pre">objects</span></code> as a field name, or if you
want to use a name other than <code class="docutils literal notranslate"><span class="pre">objects</span></code> for the <code class="docutils literal notranslate"><span class="pre">Manager</span></code>, you can rename
it on a per-model basis. To rename the <code class="docutils literal notranslate"><span class="pre">Manager</span></code> for a given class, define a
class attribute of type <code class="docutils literal notranslate"><span class="pre">models.Manager()</span></code> on that model. For example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="n">models</span>

<span class="k">class</span> <span class="nc">Person</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="c1">#...</span>
    <span class="n">people</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">Manager</span><span class="p">()</span>
</pre></div>
</div>
<p>Using this example model, <code class="docutils literal notranslate"><span class="pre">Person.objects</span></code> will generate an
<code class="docutils literal notranslate"><span class="pre">AttributeError</span></code> exception, but <code class="docutils literal notranslate"><span class="pre">Person.people.all()</span></code> will provide a list
of all <code class="docutils literal notranslate"><span class="pre">Person</span></code> objects.</p>
</div>
<div class="section" id="s-custom-managers">
<span id="s-id2"></span><span id="custom-managers"></span><span id="id2"></span><h2>Custom managers<a class="headerlink" href="#custom-managers" title="Permalink to this headline">¶</a></h2>
<p>You can use a custom <code class="docutils literal notranslate"><span class="pre">Manager</span></code> in a particular model by extending the base
<code class="docutils literal notranslate"><span class="pre">Manager</span></code> class and instantiating your custom <code class="docutils literal notranslate"><span class="pre">Manager</span></code> in your model.</p>
<p>There are two reasons you might want to customize a <code class="docutils literal notranslate"><span class="pre">Manager</span></code>: to add extra
<code class="docutils literal notranslate"><span class="pre">Manager</span></code> methods, and/or to modify the initial <code class="docutils literal notranslate"><span class="pre">QuerySet</span></code> the <code class="docutils literal notranslate"><span class="pre">Manager</span></code>
returns.</p>
<div class="section" id="s-adding-extra-manager-methods">
<span id="adding-extra-manager-methods"></span><h3>Adding extra manager methods<a class="headerlink" href="#adding-extra-manager-methods" title="Permalink to this headline">¶</a></h3>
<p>Adding extra <code class="docutils literal notranslate"><span class="pre">Manager</span></code> methods is the preferred way to add “table-level”
functionality to your models. (For “row-level” functionality – i.e., functions
that act on a single instance of a model object – use <a class="reference internal" href="models.html#model-methods"><span class="std std-ref">Model methods</span></a>, not custom <code class="docutils literal notranslate"><span class="pre">Manager</span></code> methods.)</p>
<p>A custom <code class="docutils literal notranslate"><span class="pre">Manager</span></code> method can return anything you want. It doesn’t have to
return a <code class="docutils literal notranslate"><span class="pre">QuerySet</span></code>.</p>
<p>For example, this custom <code class="docutils literal notranslate"><span class="pre">Manager</span></code> offers a method <code class="docutils literal notranslate"><span class="pre">with_counts()</span></code>, which
returns a list of all <code class="docutils literal notranslate"><span class="pre">OpinionPoll</span></code> objects, each with an extra
<code class="docutils literal notranslate"><span class="pre">num_responses</span></code> attribute that is the result of an aggregate query:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="n">models</span>

<span class="k">class</span> <span class="nc">PollManager</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Manager</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">with_counts</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="kn">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="n">connection</span>
        <span class="k">with</span> <span class="n">connection</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span> <span class="k">as</span> <span class="n">cursor</span><span class="p">:</span>
            <span class="n">cursor</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;&quot;&quot;</span>
<span class="s2">                SELECT p.id, p.question, p.poll_date, COUNT(*)</span>
<span class="s2">                FROM polls_opinionpoll p, polls_response r</span>
<span class="s2">                WHERE p.id = r.poll_id</span>
<span class="s2">                GROUP BY p.id, p.question, p.poll_date</span>
<span class="s2">                ORDER BY p.poll_date DESC&quot;&quot;&quot;</span><span class="p">)</span>
            <span class="n">result_list</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">cursor</span><span class="o">.</span><span class="n">fetchall</span><span class="p">():</span>
                <span class="n">p</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">(</span><span class="nb">id</span><span class="o">=</span><span class="n">row</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">question</span><span class="o">=</span><span class="n">row</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">poll_date</span><span class="o">=</span><span class="n">row</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span>
                <span class="n">p</span><span class="o">.</span><span class="n">num_responses</span> <span class="o">=</span> <span class="n">row</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span>
                <span class="n">result_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">result_list</span>

<span class="k">class</span> <span class="nc">OpinionPoll</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">question</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">200</span><span class="p">)</span>
    <span class="n">poll_date</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">DateField</span><span class="p">()</span>
    <span class="n">objects</span> <span class="o">=</span> <span class="n">PollManager</span><span class="p">()</span>

<span class="k">class</span> <span class="nc">Response</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">poll</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">OpinionPoll</span><span class="p">,</span> <span class="n">on_delete</span><span class="o">=</span><span class="n">models</span><span class="o">.</span><span class="n">CASCADE</span><span class="p">)</span>
    <span class="n">person_name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">50</span><span class="p">)</span>
    <span class="n">response</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TextField</span><span class="p">()</span>
</pre></div>
</div>
<p>With this example, you’d use <code class="docutils literal notranslate"><span class="pre">OpinionPoll.objects.with_counts()</span></code> to return
that list of <code class="docutils literal notranslate"><span class="pre">OpinionPoll</span></code> objects with <code class="docutils literal notranslate"><span class="pre">num_responses</span></code> attributes.</p>
<p>Another thing to note about this example is that <code class="docutils literal notranslate"><span class="pre">Manager</span></code> methods can
access <code class="docutils literal notranslate"><span class="pre">self.model</span></code> to get the model class to which they’re attached.</p>
</div>
<div class="section" id="s-modifying-a-manager-s-initial-queryset">
<span id="modifying-a-manager-s-initial-queryset"></span><h3>Modifying a manager’s initial <code class="docutils literal notranslate"><span class="pre">QuerySet</span></code><a class="headerlink" href="#modifying-a-manager-s-initial-queryset" title="Permalink to this headline">¶</a></h3>
<p>A <code class="docutils literal notranslate"><span class="pre">Manager</span></code>’s base <code class="docutils literal notranslate"><span class="pre">QuerySet</span></code> returns all objects in the system. For
example, using this model:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="n">models</span>

<span class="k">class</span> <span class="nc">Book</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">title</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
    <span class="n">author</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">50</span><span class="p">)</span>
</pre></div>
</div>
<p>…the statement <code class="docutils literal notranslate"><span class="pre">Book.objects.all()</span></code> will return all books in the database.</p>
<p>You can override a <code class="docutils literal notranslate"><span class="pre">Manager</span></code>’s base <code class="docutils literal notranslate"><span class="pre">QuerySet</span></code> by overriding the
<code class="docutils literal notranslate"><span class="pre">Manager.get_queryset()</span></code> method. <code class="docutils literal notranslate"><span class="pre">get_queryset()</span></code> should return a
<code class="docutils literal notranslate"><span class="pre">QuerySet</span></code> with the properties you require.</p>
<p>For example, the following model has <em>two</em> <code class="docutils literal notranslate"><span class="pre">Manager</span></code>s – one that returns
all objects, and one that returns only the books by Roald Dahl:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># First, define the Manager subclass.</span>
<span class="k">class</span> <span class="nc">DahlBookManager</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Manager</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">get_queryset</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">get_queryset</span><span class="p">()</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">author</span><span class="o">=</span><span class="s1">&#39;Roald Dahl&#39;</span><span class="p">)</span>

<span class="c1"># Then hook it into the Book model explicitly.</span>
<span class="k">class</span> <span class="nc">Book</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">title</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
    <span class="n">author</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">50</span><span class="p">)</span>

    <span class="n">objects</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">Manager</span><span class="p">()</span> <span class="c1"># The default manager.</span>
    <span class="n">dahl_objects</span> <span class="o">=</span> <span class="n">DahlBookManager</span><span class="p">()</span> <span class="c1"># The Dahl-specific manager.</span>
</pre></div>
</div>
<p>With this sample model, <code class="docutils literal notranslate"><span class="pre">Book.objects.all()</span></code> will return all books in the
database, but <code class="docutils literal notranslate"><span class="pre">Book.dahl_objects.all()</span></code> will only return the ones written by
Roald Dahl.</p>
<p>Of course, because <code class="docutils literal notranslate"><span class="pre">get_queryset()</span></code> returns a <code class="docutils literal notranslate"><span class="pre">QuerySet</span></code> object, you can
use <code class="docutils literal notranslate"><span class="pre">filter()</span></code>, <code class="docutils literal notranslate"><span class="pre">exclude()</span></code> and all the other <code class="docutils literal notranslate"><span class="pre">QuerySet</span></code> methods on it.
So these statements are all legal:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">Book</span><span class="o">.</span><span class="n">dahl_objects</span><span class="o">.</span><span class="n">all</span><span class="p">()</span>
<span class="n">Book</span><span class="o">.</span><span class="n">dahl_objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">title</span><span class="o">=</span><span class="s1">&#39;Matilda&#39;</span><span class="p">)</span>
<span class="n">Book</span><span class="o">.</span><span class="n">dahl_objects</span><span class="o">.</span><span class="n">count</span><span class="p">()</span>
</pre></div>
</div>
<p>This example also pointed out another interesting technique: using multiple
managers on the same model. You can attach as many <code class="docutils literal notranslate"><span class="pre">Manager()</span></code> instances to
a model as you’d like. This is an easy way to define common “filters” for your
models.</p>
<p>For example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">AuthorManager</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Manager</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">get_queryset</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">get_queryset</span><span class="p">()</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">role</span><span class="o">=</span><span class="s1">&#39;A&#39;</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">EditorManager</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Manager</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">get_queryset</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">get_queryset</span><span class="p">()</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">role</span><span class="o">=</span><span class="s1">&#39;E&#39;</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">Person</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">first_name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">50</span><span class="p">)</span>
    <span class="n">last_name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">50</span><span class="p">)</span>
    <span class="n">role</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">choices</span><span class="o">=</span><span class="p">[(</span><span class="s1">&#39;A&#39;</span><span class="p">,</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;Author&#39;</span><span class="p">)),</span> <span class="p">(</span><span class="s1">&#39;E&#39;</span><span class="p">,</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;Editor&#39;</span><span class="p">))])</span>
    <span class="n">people</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">Manager</span><span class="p">()</span>
    <span class="n">authors</span> <span class="o">=</span> <span class="n">AuthorManager</span><span class="p">()</span>
    <span class="n">editors</span> <span class="o">=</span> <span class="n">EditorManager</span><span class="p">()</span>
</pre></div>
</div>
<p>This example allows you to request <code class="docutils literal notranslate"><span class="pre">Person.authors.all()</span></code>, <code class="docutils literal notranslate"><span class="pre">Person.editors.all()</span></code>,
and <code class="docutils literal notranslate"><span class="pre">Person.people.all()</span></code>, yielding predictable results.</p>
</div>
<div class="section" id="s-default-managers">
<span id="s-id3"></span><span id="default-managers"></span><span id="id3"></span><h3>Default managers<a class="headerlink" href="#default-managers" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Model._default_manager">
<code class="descclassname">Model.</code><code class="descname">_default_manager</code><a class="headerlink" href="#django.db.models.Model._default_manager" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>If you use custom <code class="docutils literal notranslate"><span class="pre">Manager</span></code> objects, take note that the first <code class="docutils literal notranslate"><span class="pre">Manager</span></code>
Django encounters (in the order in which they’re defined in the model) has a
special status. Django interprets the first <code class="docutils literal notranslate"><span class="pre">Manager</span></code> defined in a class as
the “default” <code class="docutils literal notranslate"><span class="pre">Manager</span></code>, and several parts of Django (including
<a class="reference internal" href="../../ref/django-admin.html#django-admin-dumpdata"><code class="xref std std-djadmin docutils literal notranslate"><span class="pre">dumpdata</span></code></a>) will use that <code class="docutils literal notranslate"><span class="pre">Manager</span></code> exclusively for that model. As a
result, it’s a good idea to be careful in your choice of default manager in
order to avoid a situation where overriding <code class="docutils literal notranslate"><span class="pre">get_queryset()</span></code> results in an
inability to retrieve objects you’d like to work with.</p>
<p>You can specify a custom default manager using <a class="reference internal" href="../../ref/models/options.html#django.db.models.Options.default_manager_name" title="django.db.models.Options.default_manager_name"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Meta.default_manager_name</span></code></a>.</p>
<p>If you’re writing some code that must handle an unknown model, for example, in
a third-party app that implements a generic view, use this manager (or
<a class="reference internal" href="#django.db.models.Model._base_manager" title="django.db.models.Model._base_manager"><code class="xref py py-attr docutils literal notranslate"><span class="pre">_base_manager</span></code></a>) rather than assuming the model has an <code class="docutils literal notranslate"><span class="pre">objects</span></code>
manager.</p>
</div>
<div class="section" id="s-base-managers">
<span id="base-managers"></span><h3>Base managers<a class="headerlink" href="#base-managers" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Model._base_manager">
<code class="descclassname">Model.</code><code class="descname">_base_manager</code><a class="headerlink" href="#django.db.models.Model._base_manager" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<div class="section" id="s-using-managers-for-related-object-access">
<span id="s-managers-for-related-objects"></span><span id="using-managers-for-related-object-access"></span><span id="managers-for-related-objects"></span><h4>Using managers for related object access<a class="headerlink" href="#using-managers-for-related-object-access" title="Permalink to this headline">¶</a></h4>
<p>By default, Django uses an instance of the <code class="docutils literal notranslate"><span class="pre">Model._base_manager</span></code> manager
class when accessing related objects (i.e. <code class="docutils literal notranslate"><span class="pre">choice.question</span></code>), not the
<code class="docutils literal notranslate"><span class="pre">_default_manager</span></code> on the related object. This is because Django needs to be
able to retrieve the related object, even if it would otherwise be filtered out
(and hence be inaccessible) by the default manager.</p>
<p>If the normal base manager class (<a class="reference internal" href="#django.db.models.Manager" title="django.db.models.Manager"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.db.models.Manager</span></code></a>) isn’t
appropriate for your circumstances, you can tell Django which class to use by
setting <a class="reference internal" href="../../ref/models/options.html#django.db.models.Options.base_manager_name" title="django.db.models.Options.base_manager_name"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Meta.base_manager_name</span></code></a>.</p>
<p>Base managers aren’t used when querying on related models. For example, if the
<code class="docutils literal notranslate"><span class="pre">Question</span></code> model <a class="reference internal" href="../../intro/tutorial02.html#creating-models"><span class="std std-ref">from the tutorial</span></a> had a <code class="docutils literal notranslate"><span class="pre">deleted</span></code>
field and a base manager that filters out instances with <code class="docutils literal notranslate"><span class="pre">deleted=True</span></code>, a
queryset like <code class="docutils literal notranslate"><span class="pre">Choice.objects.filter(question__name__startswith='What')</span></code>
would include choices related to deleted questions.</p>
</div>
<div class="section" id="s-don-t-filter-away-any-results-in-this-type-of-manager-subclass">
<span id="don-t-filter-away-any-results-in-this-type-of-manager-subclass"></span><h4>Don’t filter away any results in this type of manager subclass<a class="headerlink" href="#don-t-filter-away-any-results-in-this-type-of-manager-subclass" title="Permalink to this headline">¶</a></h4>
<p>This manager is used to access objects that are related to from some other
model. In those situations, Django has to be able to see all the objects for
the model it is fetching, so that <em>anything</em> which is referred to can be
retrieved.</p>
<p>If you override the <code class="docutils literal notranslate"><span class="pre">get_queryset()</span></code> method and filter out any rows, Django
will return incorrect results. Don’t do that. A manager that filters results
in <code class="docutils literal notranslate"><span class="pre">get_queryset()</span></code> is not appropriate for use as a base manager.</p>
</div>
</div>
<div class="section" id="s-calling-custom-queryset-methods-from-the-manager">
<span id="s-calling-custom-queryset-methods-from-manager"></span><span id="calling-custom-queryset-methods-from-the-manager"></span><span id="calling-custom-queryset-methods-from-manager"></span><h3>Calling custom <code class="docutils literal notranslate"><span class="pre">QuerySet</span></code> methods from the manager<a class="headerlink" href="#calling-custom-queryset-methods-from-the-manager" title="Permalink to this headline">¶</a></h3>
<p>While most methods from the standard <code class="docutils literal notranslate"><span class="pre">QuerySet</span></code> are accessible directly from
the <code class="docutils literal notranslate"><span class="pre">Manager</span></code>, this is only the case for the extra methods defined on a
custom <code class="docutils literal notranslate"><span class="pre">QuerySet</span></code> if you also implement them on the <code class="docutils literal notranslate"><span class="pre">Manager</span></code>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">PersonQuerySet</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">QuerySet</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">authors</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">role</span><span class="o">=</span><span class="s1">&#39;A&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">editors</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">role</span><span class="o">=</span><span class="s1">&#39;E&#39;</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">PersonManager</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Manager</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">get_queryset</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">PersonQuerySet</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="n">using</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">_db</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">authors</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_queryset</span><span class="p">()</span><span class="o">.</span><span class="n">authors</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">editors</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_queryset</span><span class="p">()</span><span class="o">.</span><span class="n">editors</span><span class="p">()</span>

<span class="k">class</span> <span class="nc">Person</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">first_name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">50</span><span class="p">)</span>
    <span class="n">last_name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">50</span><span class="p">)</span>
    <span class="n">role</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">choices</span><span class="o">=</span><span class="p">[(</span><span class="s1">&#39;A&#39;</span><span class="p">,</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;Author&#39;</span><span class="p">)),</span> <span class="p">(</span><span class="s1">&#39;E&#39;</span><span class="p">,</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;Editor&#39;</span><span class="p">))])</span>
    <span class="n">people</span> <span class="o">=</span> <span class="n">PersonManager</span><span class="p">()</span>
</pre></div>
</div>
<p>This example allows you to call both <code class="docutils literal notranslate"><span class="pre">authors()</span></code> and <code class="docutils literal notranslate"><span class="pre">editors()</span></code> directly from
the manager <code class="docutils literal notranslate"><span class="pre">Person.people</span></code>.</p>
</div>
<div class="section" id="s-creating-a-manager-with-queryset-methods">
<span id="s-create-manager-with-queryset-methods"></span><span id="creating-a-manager-with-queryset-methods"></span><span id="create-manager-with-queryset-methods"></span><h3>Creating a manager with <code class="docutils literal notranslate"><span class="pre">QuerySet</span></code> methods<a class="headerlink" href="#creating-a-manager-with-queryset-methods" title="Permalink to this headline">¶</a></h3>
<p>In lieu of the above approach which requires duplicating methods on both the
<code class="docutils literal notranslate"><span class="pre">QuerySet</span></code> and the <code class="docutils literal notranslate"><span class="pre">Manager</span></code>, <a class="reference internal" href="../../ref/models/querysets.html#django.db.models.query.QuerySet.as_manager" title="django.db.models.query.QuerySet.as_manager"><code class="xref py py-meth docutils literal notranslate"><span class="pre">QuerySet.as_manager()</span></code></a> can be used to create an instance
of <code class="docutils literal notranslate"><span class="pre">Manager</span></code> with a copy of a custom <code class="docutils literal notranslate"><span class="pre">QuerySet</span></code>’s methods:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Person</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="o">...</span>
    <span class="n">people</span> <span class="o">=</span> <span class="n">PersonQuerySet</span><span class="o">.</span><span class="n">as_manager</span><span class="p">()</span>
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">Manager</span></code> instance created by <a class="reference internal" href="../../ref/models/querysets.html#django.db.models.query.QuerySet.as_manager" title="django.db.models.query.QuerySet.as_manager"><code class="xref py py-meth docutils literal notranslate"><span class="pre">QuerySet.as_manager()</span></code></a> will be virtually
identical to the <code class="docutils literal notranslate"><span class="pre">PersonManager</span></code> from the previous example.</p>
<p>Not every <code class="docutils literal notranslate"><span class="pre">QuerySet</span></code> method makes sense at the <code class="docutils literal notranslate"><span class="pre">Manager</span></code> level; for
instance we intentionally prevent the <a class="reference internal" href="../../ref/models/querysets.html#django.db.models.query.QuerySet.delete" title="django.db.models.query.QuerySet.delete"><code class="xref py py-meth docutils literal notranslate"><span class="pre">QuerySet.delete()</span></code></a> method from being copied onto
the <code class="docutils literal notranslate"><span class="pre">Manager</span></code> class.</p>
<p>Methods are copied according to the following rules:</p>
<ul class="simple">
<li>Public methods are copied by default.</li>
<li>Private methods (starting with an underscore) are not copied by default.</li>
<li>Methods with a <code class="docutils literal notranslate"><span class="pre">queryset_only</span></code> attribute set to <code class="docutils literal notranslate"><span class="pre">False</span></code> are always copied.</li>
<li>Methods with a <code class="docutils literal notranslate"><span class="pre">queryset_only</span></code> attribute set to <code class="docutils literal notranslate"><span class="pre">True</span></code> are never copied.</li>
</ul>
<p>For example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">CustomQuerySet</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">QuerySet</span><span class="p">):</span>
    <span class="c1"># Available on both Manager and QuerySet.</span>
    <span class="k">def</span> <span class="nf">public_method</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span>

    <span class="c1"># Available only on QuerySet.</span>
    <span class="k">def</span> <span class="nf">_private_method</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span>

    <span class="c1"># Available only on QuerySet.</span>
    <span class="k">def</span> <span class="nf">opted_out_public_method</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span>
    <span class="n">opted_out_public_method</span><span class="o">.</span><span class="n">queryset_only</span> <span class="o">=</span> <span class="kc">True</span>

    <span class="c1"># Available on both Manager and QuerySet.</span>
    <span class="k">def</span> <span class="nf">_opted_in_private_method</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span>
    <span class="n">_opted_in_private_method</span><span class="o">.</span><span class="n">queryset_only</span> <span class="o">=</span> <span class="kc">False</span>
</pre></div>
</div>
<div class="section" id="s-from-queryset">
<span id="from-queryset"></span><h4><code class="docutils literal notranslate"><span class="pre">from_queryset()</span></code><a class="headerlink" href="#from-queryset" title="Permalink to this headline">¶</a></h4>
<dl class="classmethod">
<dt id="django.db.models.from_queryset">
<em class="property">classmethod </em><code class="descname">from_queryset</code>(<em>queryset_class</em>)<a class="headerlink" href="#django.db.models.from_queryset" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>For advanced usage you might want both a custom <code class="docutils literal notranslate"><span class="pre">Manager</span></code> and a custom
<code class="docutils literal notranslate"><span class="pre">QuerySet</span></code>. You can do that by calling <code class="docutils literal notranslate"><span class="pre">Manager.from_queryset()</span></code> which
returns a <em>subclass</em> of your base <code class="docutils literal notranslate"><span class="pre">Manager</span></code> with a copy of the custom
<code class="docutils literal notranslate"><span class="pre">QuerySet</span></code> methods:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">BaseManager</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Manager</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">manager_only_method</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span>

<span class="k">class</span> <span class="nc">CustomQuerySet</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">QuerySet</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">manager_and_queryset_method</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span>

<span class="k">class</span> <span class="nc">MyModel</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">objects</span> <span class="o">=</span> <span class="n">BaseManager</span><span class="o">.</span><span class="n">from_queryset</span><span class="p">(</span><span class="n">CustomQuerySet</span><span class="p">)()</span>
</pre></div>
</div>
<p>You may also store the generated class into a variable:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">CustomManager</span> <span class="o">=</span> <span class="n">BaseManager</span><span class="o">.</span><span class="n">from_queryset</span><span class="p">(</span><span class="n">CustomQuerySet</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">MyModel</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">objects</span> <span class="o">=</span> <span class="n">CustomManager</span><span class="p">()</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="s-custom-managers-and-model-inheritance">
<span id="s-custom-managers-and-inheritance"></span><span id="custom-managers-and-model-inheritance"></span><span id="custom-managers-and-inheritance"></span><h3>Custom managers and model inheritance<a class="headerlink" href="#custom-managers-and-model-inheritance" title="Permalink to this headline">¶</a></h3>
<p>Here’s how Django handles custom managers and <a class="reference internal" href="models.html#model-inheritance"><span class="std std-ref">model inheritance</span></a>:</p>
<ol class="arabic simple">
<li>Managers from base classes are always inherited by the child class,
using Python’s normal name resolution order (names on the child
class override all others; then come names on the first parent class,
and so on).</li>
<li>If no managers are declared on a model and/or its parents, Django
automatically creates the <code class="docutils literal notranslate"><span class="pre">objects</span></code> manager.</li>
<li>The default manager on a class is either the one chosen with
<a class="reference internal" href="../../ref/models/options.html#django.db.models.Options.default_manager_name" title="django.db.models.Options.default_manager_name"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Meta.default_manager_name</span></code></a>, or the first manager
declared on the model, or the default manager of the first parent model.</li>
</ol>
<p>These rules provide the necessary flexibility if you want to install a
collection of custom managers on a group of models, via an abstract base
class, but still customize the default manager. For example, suppose you have
this base class:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">AbstractBase</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="c1"># ...</span>
    <span class="n">objects</span> <span class="o">=</span> <span class="n">CustomManager</span><span class="p">()</span>

    <span class="k">class</span> <span class="nc">Meta</span><span class="p">:</span>
        <span class="n">abstract</span> <span class="o">=</span> <span class="kc">True</span>
</pre></div>
</div>
<p>If you use this directly in a subclass, <code class="docutils literal notranslate"><span class="pre">objects</span></code> will be the default
manager if you declare no managers in the base class:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">ChildA</span><span class="p">(</span><span class="n">AbstractBase</span><span class="p">):</span>
    <span class="c1"># ...</span>
    <span class="c1"># This class has CustomManager as the default manager.</span>
    <span class="k">pass</span>
</pre></div>
</div>
<p>If you want to inherit from <code class="docutils literal notranslate"><span class="pre">AbstractBase</span></code>, but provide a different default
manager, you can provide the default manager on the child class:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">ChildB</span><span class="p">(</span><span class="n">AbstractBase</span><span class="p">):</span>
    <span class="c1"># ...</span>
    <span class="c1"># An explicit default manager.</span>
    <span class="n">default_manager</span> <span class="o">=</span> <span class="n">OtherManager</span><span class="p">()</span>
</pre></div>
</div>
<p>Here, <code class="docutils literal notranslate"><span class="pre">default_manager</span></code> is the default. The <code class="docutils literal notranslate"><span class="pre">objects</span></code> manager is
still available, since it’s inherited. It just isn’t used as the default.</p>
<p>Finally for this example, suppose you want to add extra managers to the child
class, but still use the default from <code class="docutils literal notranslate"><span class="pre">AbstractBase</span></code>. You can’t add the new
manager directly in the child class, as that would override the default and you would
have to also explicitly include all the managers from the abstract base class.
The solution is to put the extra managers in another base class and introduce
it into the inheritance hierarchy <em>after</em> the defaults:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">ExtraManager</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">extra_manager</span> <span class="o">=</span> <span class="n">OtherManager</span><span class="p">()</span>

    <span class="k">class</span> <span class="nc">Meta</span><span class="p">:</span>
        <span class="n">abstract</span> <span class="o">=</span> <span class="kc">True</span>

<span class="k">class</span> <span class="nc">ChildC</span><span class="p">(</span><span class="n">AbstractBase</span><span class="p">,</span> <span class="n">ExtraManager</span><span class="p">):</span>
    <span class="c1"># ...</span>
    <span class="c1"># Default manager is CustomManager, but OtherManager is</span>
    <span class="c1"># also available via the &quot;extra_manager&quot; attribute.</span>
    <span class="k">pass</span>
</pre></div>
</div>
<p>Note that while you can <em>define</em> a custom manager on the abstract model, you
can’t <em>invoke</em> any methods using the abstract model. That is:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">ClassA</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">do_something</span><span class="p">()</span>
</pre></div>
</div>
<p>is legal, but:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">AbstractBase</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">do_something</span><span class="p">()</span>
</pre></div>
</div>
<p>will raise an exception. This is because managers are intended to encapsulate
logic for managing collections of objects. Since you can’t have a collection of
abstract objects, it doesn’t make sense to be managing them. If you have
functionality that applies to the abstract model, you should put that functionality
in a <code class="docutils literal notranslate"><span class="pre">staticmethod</span></code> or <code class="docutils literal notranslate"><span class="pre">classmethod</span></code> on the abstract model.</p>
</div>
<div class="section" id="s-implementation-concerns">
<span id="implementation-concerns"></span><h3>Implementation concerns<a class="headerlink" href="#implementation-concerns" title="Permalink to this headline">¶</a></h3>
<p>Whatever features you add to your custom <code class="docutils literal notranslate"><span class="pre">Manager</span></code>, it must be
possible to make a shallow copy of a <code class="docutils literal notranslate"><span class="pre">Manager</span></code> instance; i.e., the
following code must work:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">copy</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">manager</span> <span class="o">=</span> <span class="n">MyManager</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">my_copy</span> <span class="o">=</span> <span class="n">copy</span><span class="o">.</span><span class="n">copy</span><span class="p">(</span><span class="n">manager</span><span class="p">)</span>
</pre></div>
</div>
<p>Django makes shallow copies of manager objects during certain queries;
if your Manager cannot be copied, those queries will fail.</p>
<p>This won’t be an issue for most custom managers. If you are just
adding simple methods to your <code class="docutils literal notranslate"><span class="pre">Manager</span></code>, it is unlikely that you
will inadvertently make instances of your <code class="docutils literal notranslate"><span class="pre">Manager</span></code> uncopyable.
However, if you’re overriding <code class="docutils literal notranslate"><span class="pre">__getattr__</span></code> or some other private
method of your <code class="docutils literal notranslate"><span class="pre">Manager</span></code> object that controls object state, you
should ensure that you don’t affect the ability of your <code class="docutils literal notranslate"><span class="pre">Manager</span></code> to
be copied.</p>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Managers</a><ul>
<li><a class="reference internal" href="#manager-names">Manager names</a></li>
<li><a class="reference internal" href="#custom-managers">Custom managers</a><ul>
<li><a class="reference internal" href="#adding-extra-manager-methods">Adding extra manager methods</a></li>
<li><a class="reference internal" href="#modifying-a-manager-s-initial-queryset">Modifying a manager’s initial <code class="docutils literal notranslate"><span class="pre">QuerySet</span></code></a></li>
<li><a class="reference internal" href="#default-managers">Default managers</a></li>
<li><a class="reference internal" href="#base-managers">Base managers</a><ul>
<li><a class="reference internal" href="#using-managers-for-related-object-access">Using managers for related object access</a></li>
<li><a class="reference internal" href="#don-t-filter-away-any-results-in-this-type-of-manager-subclass">Don’t filter away any results in this type of manager subclass</a></li>
</ul>
</li>
<li><a class="reference internal" href="#calling-custom-queryset-methods-from-the-manager">Calling custom <code class="docutils literal notranslate"><span class="pre">QuerySet</span></code> methods from the manager</a></li>
<li><a class="reference internal" href="#creating-a-manager-with-queryset-methods">Creating a manager with <code class="docutils literal notranslate"><span class="pre">QuerySet</span></code> methods</a><ul>
<li><a class="reference internal" href="#from-queryset"><code class="docutils literal notranslate"><span class="pre">from_queryset()</span></code></a></li>
</ul>
</li>
<li><a class="reference internal" href="#custom-managers-and-model-inheritance">Custom managers and model inheritance</a></li>
<li><a class="reference internal" href="#implementation-concerns">Implementation concerns</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="search.html"
                        title="previous chapter">Search</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="sql.html"
                        title="next chapter">Performing raw SQL queries</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../../_sources/topics/db/managers.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Mar 04, 2020</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="search.html" title="Search">previous</a>
     |
    <a href="../index.html" title="Using Django" accesskey="U">up</a>
   |
    <a href="sql.html" title="Performing raw SQL queries">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>